﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UbbTimetable2
{
    /// <summary>
    /// A form for class name manipulation.
    /// </summary>
    public partial class ClassnameForm : Form
    {
        private MainForm _parent;
        private ClassesDao _classesDao;

        /// <summary>
        /// Main constructor.
        /// </summary>
        /// <param name="parent">The parent MainForm that will be frozen.</param>
        public ClassnameForm(MainForm parent)
        {
            InitializeComponent();

            _parent = parent;
            _parent.Enabled = false;
            _classesDao = new ClassesDao();
        }

        private void ClassnameForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _parent.Enabled = true;
        }

        /// <summary>
        /// Fills the form with data.
        /// </summary>
        private void LoadAll()
        {
            Utilities.FillDgv(_classesDao, dgvClassnames, Utilities.CLASSNAMES);
        }

        private void ClassnameForm_Load(object sender, EventArgs e)
        {
            LoadAll();

            SetInputStates(false);
            cbNew.Enabled = false;
        }

        /// <summary>
        /// Sets the input widgets state.
        /// </summary>
        /// <param name="state">The new state.</param>
        private void SetInputStates(bool state)
        {
            tbName.Enabled = state;
            btSave.Enabled = state;
            btCancel.Enabled = state;

            dgvClassnames.Enabled = !state;
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            cbNew.Checked = true;
            SetInputStates(true);
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            cbNew.Checked = false;
            SetInputStates(false);
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            if (dgvClassnames.SelectedRows.Count == 1)
            {
                tbName.Text = dgvClassnames.SelectedRows[0].Cells[1].Value.ToString();

                SetInputStates(true);
            }
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (!CorrectInput())
            {
                MessageBox.Show("The input is not correct!");
            }
            else
            {
                if (cbNew.Checked)
                {
                    _classesDao.InsertClassName(tbName.Text);
                }
                else
                {
                    _classesDao.UpdateClassName(Convert.ToInt32(dgvClassnames.SelectedRows[0].Cells[0].Value),
                        tbName.Text);
                }
                cbNew.Checked = false;
                SetInputStates(false);
                Utilities.FillDgv(_classesDao, dgvClassnames, Utilities.CLASSNAMES);
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (dgvClassnames.SelectedRows.Count == 1)
            {
                DialogResult dlg = MessageBox.Show("Delete item?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlg == DialogResult.Yes)
                {
                    _classesDao.DeleteClassName(Convert.ToInt32(dgvClassnames.SelectedRows[0].Cells[0].Value));
                }
                Utilities.FillDgv(_classesDao, dgvClassnames, Utilities.CLASSNAMES);
            }
        }

        /// <summary>
        /// Tells if the input textboxes have correct values.
        /// </summary>
        /// <returns>True if the input is correct and false otherwise.</returns>
        private bool CorrectInput()
        {
            return tbName.Text != "";
        }
    }
}
